//
//  TLShortStoryHeadTableViewCell.m
//  WXReader
//
//  Created by huaya on 2025/1/14.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLShortStoryHeadTableViewCell.h"

@interface TLShortStoryHeadTableViewCell ()

@property (weak, nonatomic) IBOutlet UILabel *bookNameLab;
@property (weak, nonatomic) IBOutlet UIButton *collectBtn;
@property (weak, nonatomic) IBOutlet UILabel *collectNumbLab;
@property (weak, nonatomic) IBOutlet UIView *descView;
@property (weak, nonatomic) IBOutlet UILabel *descLab;

@property (nonatomic, strong) NSMutableArray *tipsLabMArr;

@end

@implementation TLShortStoryHeadTableViewCell
-(NSMutableArray *)tipsLabMArr{
    
    if (!_tipsLabMArr) {
        _tipsLabMArr = [NSMutableArray new];
    }
    return _tipsLabMArr;
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(void)setDataModel:(TLBookListForSSModel *)dataModel{
    if (dataModel) {
        _dataModel = dataModel;
        
//        _bookNameLab.text = dataModel.worksName;
        
        [self.bookNameLab attributedStringWithHTMLText:dataModel.worksName fontSize:22 characterSpacing:0 lineSpacing:0 paragraphSpacing:0 alignment:NSTextAlignmentLeft];
        
        _collectNumbLab.text = [NSString stringWithFormat:@"%@",dataModel.collectNum];
        
        self.descView.hidden = NO;
        
//        _descLab.text = dataModel.desc;
        
        [self.descLab attributedStringWithHTMLText:dataModel.desc fontSize:[MKReaderCfg shared].fontSize.intValue characterSpacing:[MKReaderCfg shared].characterSpacingSize.intValue lineSpacing:[MKReaderCfg shared].lineSpacing.intValue paragraphSpacing:[MKReaderCfg shared].paragraphSpacing.intValue alignment:NSTextAlignmentLeft];
        
        _collectBtn.selected = dataModel.collectStatus;
        
        [self createTagWithTags:dataModel.tagsList];
        
        if ([MKReaderCfg shared].bgColorIndex.intValue == 0) {
            //白
            
            [self.contentView setBackgroundColor:[UIColor hex_F6F6F6]];
            self.bookNameLab.textColor = [UIColor blackColor];
            self.descLab.textColor = [UIColor hex_666666];
        }else{
            //黑
            
            [self.contentView setBackgroundColor:[UIColor hex_191820]];
            self.bookNameLab.textColor = [UIColor whiteColor];
            self.descLab.textColor = [UIColor hex_999999];
            
        }
        
    }
}
- (void)createTagWithTags:(NSArray<TLTagsModel *> *)tags{
    
    CGFloat x = 8;
    
    for (UILabel *lab in self.tipsLabMArr) {
        [lab removeFromSuperview];
    }
    [self.tipsLabMArr removeAllObjects];
    
    for (int i=0; i<tags.count; i++) {
        
        if (i>=2) {
            break;
        }
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(x, CGRectGetMaxY(self.bookNameLab.frame)+20, 75, 20)];
        
        lab.font =  [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
        
        if ([MKReaderCfg shared].bgColorIndex.intValue == 0) {
            lab.textColor = [UIColor hex_999999];
            lab.backgroundColor = [UIColor hex_F2F2F2];
        }else{
            lab.textColor = [UIColor whiteColor];
            lab.backgroundColor = [UIColor hex_313538];
        }
        
        lab.text = [NSString stringWithFormat:@"  %@  ",tags[i].tagName];
        [lab sizeToFit];
        CGRect frame = lab.frame;
        frame.size.height = 20;
        lab.frame = frame;
        
        lab.layer.masksToBounds = YES;
        lab.layer.cornerRadius = 10;
        
        if ([MKReaderCfg shared].bgColorIndex.intValue == 0) {
            lab.textColor = [UIColor hex_999999];
            lab.backgroundColor = [UIColor hex_F2F2F2];
        }else{
            lab.textColor = [UIColor hex_999999];
            lab.backgroundColor = [UIColor hex_313538];
        }
        
        x = x + frame.size.width + 8;
        
        [self.contentView addSubview:lab];
        [self.tipsLabMArr addObject:lab];
        
    }
    
}
- (void)setNextModel:(TLSSNextModel *)nextModel{
    if (nextModel) {
        
        _nextModel = nextModel;
        
        _bookNameLab.text = nextModel.worksName;
        _collectNumbLab.text = [NSString stringWithFormat:@"%@",nextModel.collectNum];
        
        _collectBtn.selected = nextModel.collectStatus;
        
        [self createTagWithTags:nextModel.tagsList];
        
        self.descView.hidden = YES;
    }
}
- (IBAction)collectionAction:(id)sender {
    
    if (self.collectBlock) {
        self.collectBlock(self.dataModel.worksId);
    }
    
}
@end
